package com.tmt.im.service.helper;

import com.tmt.im.common.Constant;
import com.tmt.im.common.enums.ChatSessionType;
import com.tmt.im.common.pojo.ChatSessionPoJo;
import com.tmt.im.service.controller.vo.UpdateSessionRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/28 17:00
 * @Version 1.0
 **/
public class UpdateSessionVoHelper {

    public static MultiValueMap<String, Object> convertTo(Long currentUserId, ChatSessionType type, UpdateSessionRequest vo) {
        if ("V9".equalsIgnoreCase(Constant.YUN_XIN_API_VERSION)) {
            return convertTo_V9(currentUserId, type, vo);
        }
        MultiValueMap<String, Object> map = convertTo_V10(vo);
        map.add("operator_id", "" + currentUserId);
        map.add("team_type", Math.max(1, type.getType()));
        return map;
    }

    public static MultiValueMap<String, Object> convertTo_V9(Long currentUserId, ChatSessionType type, UpdateSessionRequest vo) {
        MultiValueMap<String, Object> result = new LinkedMultiValueMap<>();
        result.add("tid", vo.getTid());
        result.add("owner", "" + currentUserId);

        if (StringUtils.hasText(vo.getName())) {
            result.add("tname", vo.getName());
        }
        if (StringUtils.hasText(vo.getAvatar())) {
            result.add("icon", vo.getAvatar());
        }
        if (StringUtils.hasText(vo.getAnnouncement())) {
            result.add("announcement", vo.getAnnouncement());
        }
        if (StringUtils.hasText(vo.getIntroduce())) {
            result.add("intro", vo.getIntroduce());
        }

        if (vo.getMembersLimit() != null) {
            result.add("teamMemberLimit", Math.min(200, Math.max(2, vo.getMembersLimit())));
        }
        if (StringUtils.hasText(vo.getServerExtension())) {
            result.add("custom", vo.getServerExtension());
        }
        if (StringUtils.hasText(vo.getExtension())) {
            result.add("attach", vo.getExtension());
        }

        if (vo.getJoinMode() != null) {
            int joinMode = Boolean.TRUE.equals(vo.getJoinMode()) ? 0 : 1;
            result.add("join_mode", joinMode);
        }
        if (vo.getBeInvitedMode() != null) {
            result.add("beinvitemode", vo.getBeInvitedMode());
        }
        if (vo.getInviteMode() != null) {
            result.add("invitemode", vo.getInviteMode());
        }
        if (vo.getUpdateTeamInfoMode() != null) {
            result.add("uptinfomode", vo.getUpdateTeamInfoMode());
        }
        if (vo.getUpdateExtensionMode() != null) {
            result.add("upcustommode", vo.getUpdateExtensionMode());
        }
        return result;
    }

    public static MultiValueMap<String, Object> convertTo_V10(UpdateSessionRequest vo) {
        MultiValueMap<String, Object> result = new LinkedMultiValueMap<>();
        if (StringUtils.hasText(vo.getName())) {
            result.add("name", vo.getName());
        }
        if (StringUtils.hasText(vo.getAnnouncement())) {
            result.add("announcement", vo.getAnnouncement());
        }
        if (StringUtils.hasText(vo.getIntroduce())) {
            result.add("intro", vo.getIntroduce());
        }
        if (vo.getMembersLimit() != null) {
            result.add("members_limit", vo.getMembersLimit());
        }
        if (StringUtils.hasText(vo.getServerExtension())) {
            result.add("server_extension", vo.getServerExtension());
        }
        if (StringUtils.hasText(vo.getExtension())) {
            result.add("extension", vo.getExtension());
        }

        Map<String, Object> configuration = new LinkedHashMap<>();
        result.add("configuration", configuration);

        if (vo.getJoinMode() != null) {
            configuration.put("join_mode", vo.getJoinMode());
        }
        if (vo.getBeInvitedMode() != null) {
            configuration.put("agree_mode", vo.getBeInvitedMode());
        }
        if (vo.getInviteMode() != null) {
            configuration.put("invite_mode", vo.getInviteMode());
        }
        if (vo.getUpdateTeamInfoMode() != null) {
            configuration.put("update_team_info_mode", vo.getUpdateTeamInfoMode());
        }
        if (vo.getUpdateExtensionMode() != null) {
            configuration.put("update_extension_mode", vo.getUpdateExtensionMode());
        }
        return result;
    }

    public static void copyProperties(UpdateSessionRequest sessionVo, ChatSessionPoJo sessionPoJo) {

        if (StringUtils.hasText(sessionVo.getName())) {
            sessionPoJo.setName(sessionVo.getName());
        }
        if (StringUtils.hasText(sessionVo.getAnnouncement())) {
            sessionPoJo.setAnnouncement(sessionVo.getAnnouncement());
        }
        if (StringUtils.hasText(sessionVo.getIntroduce())) {
            sessionPoJo.setIntroduce(sessionVo.getIntroduce());
        }
        if (sessionVo.getMembersLimit() != null) {
            sessionPoJo.setMemberAmountLimit(sessionVo.getMembersLimit());
        }

        if (sessionVo.getJoinMode() != null) {
            int joinMode = Boolean.TRUE.equals(sessionVo.getJoinMode()) ? 0 : 1;
            sessionPoJo.setJoinMode(joinMode);
        }

        if (sessionVo.getBeInvitedMode() != null) {
            sessionPoJo.setBeInvitedMode(sessionVo.getBeInvitedMode());
        }

        if (sessionVo.getInviteMode() != null) {
            sessionPoJo.setInviteMode(sessionVo.getInviteMode());
        }

        if (sessionVo.getUpdateTeamInfoMode() != null) {
            sessionPoJo.setUpdateTeamInfoMode(sessionVo.getUpdateTeamInfoMode());
        }

        if (sessionVo.getUpdateExtensionMode() != null) {
            sessionPoJo.setUpdateExtensionMode(sessionVo.getUpdateExtensionMode());
        }

        if (sessionVo.getChatBannedMode() != null) {
            int muteType = Boolean.TRUE.equals(sessionVo.getChatBannedMode()) ? 0 : 3;
            sessionPoJo.setChatBanned(muteType);
        }

        if (sessionVo.getForbidAddFriend() != null) {
            sessionPoJo.setForbidAddFriend(sessionVo.getForbidAddFriend());
        }

        if (sessionVo.getNonCollectionState() != null) {
            sessionPoJo.setAllowRedPackage(sessionVo.getNonCollectionState());
        }
    }

}
